package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.SearchListBean;
import com.wwp.tbshop.util.Constant;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Xiaokun on 2017/6/19.
 */

public class SearchRVAdapter extends BaseAdapter {

    private Context mContext;
    private List<SearchListBean.ProductListBean> mProductList;

    public SearchRVAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<SearchListBean.ProductListBean> productList) {
        this.mProductList = productList;
    }

    @Override
    public int getCount() {
        if (mProductList!=null&&mProductList.size()>0){
            return mProductList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchHolder holder=null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_collect, parent, false);
            holder=new SearchHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (SearchHolder) convertView.getTag();
        }
        holder.setData(mProductList.get(position));
        return convertView;
    }


    public class SearchHolder {
        @Bind(R.id.iv_goods_pic)
        ImageView ivGoodsPic;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @Bind(R.id.tv_old_price)
        TextView tvOldPrice;

        SearchHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(SearchListBean.ProductListBean productList) {
            tvGoodsName.setText(productList.getName());
            tvGoodsPrice.setText("￥" + productList.getPrice());
            tvGoodsPrice.setTextColor(Color.RED);
            tvOldPrice.setText("￥" + productList.getMarketPrice());
            tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            Picasso.with(mContext).load(Constant.HOST + productList.getPic()).into(ivGoodsPic);
        }
    }
}
